package cn.sytton.taffecloud.common.sms.alibaba;

import cn.sytton.taffecloud.common.base.exception.BizException;
import cn.sytton.taffecloud.common.base.sms.ILoginSmsCode;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.LinkedHashMap;

/**
 * 登录短信验证码实现类
 *
 * @author skyrock
 */
@Slf4j
@Component
public class LoginSmsCode implements ILoginSmsCode, InitializingBean {

    @Value("${auth.sms.alibaba.accessKeyId:}")
    private String accessKeyId;
    @Value("${auth.sms.alibaba.accessKeySecret:}")
    private String accessKeySecret;
    @Value("${auth.sms.alibaba.signName:}")
    private String signName;
    @Value("${auth.sms.alibaba.templateCode:}")
    private String templateCode;

    private com.aliyun.dysmsapi20170525.Client client;

    private com.aliyun.dysmsapi20170525.Client createClient() {
        if (client == null) {
            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                    // 您的 AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 您的 AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
            // 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            try {
                client = new com.aliyun.dysmsapi20170525.Client(config);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new BizException("阿里短信客户端创建错误");
            }
        }
        return client;
    }

    public String send(String phoneNumber, LinkedHashMap<String, Object> params) {
        client = createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest();
        sendSmsRequest.setPhoneNumbers(phoneNumber);
        sendSmsRequest.setSignName(signName);
        sendSmsRequest.setTemplateCode(templateCode);
        sendSmsRequest.setTemplateParam((new Gson()).toJson(params));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            if (response.getStatusCode() == 200 && "OK".equalsIgnoreCase(response.getBody().getCode())) {
                String resp = new Gson().toJson(response);
                log.info(phoneNumber + " -> " +resp);
                return resp;
            } else {
                log.error(new Gson().toJson(response));
                throw new BizException("获取短信验证码错误");
            }
        } catch (Exception _error) {
            log.error(_error.getMessage(), _error);
            throw new BizException("获取短信验证码错误");
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (!StringUtils.hasText(accessKeyId)) {
            log.warn("阿里验证码参数[auth.sms.alibaba.accessKeyId]为空");
        }
        if (!StringUtils.hasText(accessKeySecret)) {
            log.warn("阿里验证码参数[auth.sms.alibaba.accessKeySecret]为空");
        }
        if (!StringUtils.hasText(signName)) {
            log.warn("阿里验证码参数[auth.sms.alibaba.signName]为空");
        }
        if (!StringUtils.hasText(templateCode)) {
            log.warn("阿里验证码参数[auth.sms.alibaba.templateCode]为空");
        }
    }
}
